﻿using System;
using System.Collections.Generic;

using ActiveDocument;
using ActiveDocument.Persistence;
using ActiveDocument.SearchEngine;
using ActiveDocument.Queries;

namespace ActiveDocument
{
    public class ActiveDocumentBase<T> where T : class, new()
    {
        public static List<T> FindAll()
        {
            return FindAll(null);
        }
        public static List<T> FindAll(Order[] order)
        {
            return _Find(null, order, null, null);
        }

        public static List<T> Find(int start, int stop)
        {
            return _Find(null, null, start, stop);
        }
        public static List<T> Find(IQuery Query)
        {
            return _Find(Query, null, null, null);
        }
        public static List<T> Find(IQuery Query, int start, int stop)
        {
            return _Find(Query, null, start, stop);
        }
        public static List<T> Find(Order[] order, int start, int stop)
        {
            return _Find(null, order, start, stop);
        }
        public static List<T> Find(IQuery Query, Order[] order)
        {
            return _Find(Query, order, null, null);
        }
        public static List<T> Find(IQuery Query, Order[] order, int start, int stop)
        {
            return _Find(Query, order, start, stop);
        }
        private static List<T> _Find(IQuery Query, Order[] order, int? start, int? stop)
        {
            SearchEngine.SearchEngine engine = new SearchEngine.SearchEngine();
            return engine.Search<T>(Query, order, start, stop);
        }

        public static T FindByID(object ID)
        {
            return new T();
        }

        public static T FindFirst()
        {
            return FindFirst(null);
        }
        public static T FindFirst(IQuery Query)
        {
            List<T> results = _Find(Query, null, 1, 2);
            return results.Count == 1 ? results[0] : null;
        }

        public void Add()
        {
            SearchEngine.SearchEngineIndex.Add(ActiveDocumentConverter.ConvertToDocument(this));
        }
        public void Update()
        {

        }
        public void Delete()
        {

        }
    }
}